package com.afd.ozblip.activity;


import com.afd.ozblip.animation.CouponTabAnimationListener;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class CouponActivity extends TabActivity implements View.OnClickListener {

	/**
	 * TabHost控件
	 */
	public TabHost mTabHost;
	/**
	 * Tab文字
	 */
	private TextView tabText[];
	/**
	 * Tab图片
	 */
	private ImageView tabImage[];
	// tab索引
	public int oldIndex;
	public int nowIndex;

	private static final String INTENT_TAB_CHANGE = "INTENT_TAB_CHANGE";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.coupon_tab);
		oldIndex = -1;
		nowIndex = 0;
		makeTab();
	}

	private void makeTab() {
		Intent nearby = new Intent(this, NearbyCouponActivity.class);
		Intent city = new Intent(this, CityCouponActivity.class);
		Intent hot = new Intent(this, HotCouponActivity.class);

		mTabHost = getTabHost();
		mTabHost.addTab(addSpec("mhome_tab", R.string.index_bottom_home,
				R.drawable.nav_menu_search_style, nearby));
		mTabHost.addTab(addSpec("mhot_tab", R.string.index_bottom_home,
				R.drawable.nav_menu_top_style, city));
		mTabHost.addTab(addSpec("msort_tab", R.string.index_bottom_home,
				R.drawable.nav_menu_sign_style, hot));

		tabText = new TextView[3];
		tabText[0] = (TextView) findViewById(R.id.tv_main_menu_text_home);
		tabText[1] = (TextView) findViewById(R.id.tv_main_menu_text_category);
		tabText[2] = (TextView) findViewById(R.id.tv_main_menu_text_me);

		tabImage = new ImageView[3];
		tabImage[0] = (ImageView) findViewById(R.id.img_main_menu_bg_home);
		tabImage[1] = (ImageView) findViewById(R.id.img_main_menu_bg_category);
		tabImage[2] = (ImageView) findViewById(R.id.img_main_menu_bg_me);
		for (int i = 0; i < 3; i++) {
			tabText[i].setOnClickListener(this);
		}

	}

	public TextView[] getTabTextArr() {
		return this.tabText;
	}

	public ImageView[] getTabImageArr() {
		return this.tabImage;
	}

	private TabHost.TabSpec addSpec(String paramString, int paramInt1,
			int paramInt2, Intent paramIntent) {
		return mTabHost
				.newTabSpec(paramString)
				.setIndicator(getString(paramInt1),
						getResources().getDrawable(paramInt2))
				.setContent(paramIntent);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		int tabIndex = intent.getIntExtra(INTENT_TAB_CHANGE, -1);
		if (tabIndex != -1) {
			getTabHost().setCurrentTab(tabIndex);
		}
	}

	@Override
	public void onClick(View v) {
		oldIndex = nowIndex;
		TranslateAnimation translateanimation = new TranslateAnimation(0.0F,
				v.getLeft() - tabText[oldIndex].getLeft(), 0.0F, 0.0F);
		translateanimation.setDuration(200L);
		translateanimation.setFillAfter(false);
		translateanimation.setFillBefore(false);
		tabImage[oldIndex].startAnimation(translateanimation);
		translateanimation.setAnimationListener(new CouponTabAnimationListener(this,
				v));
	}
}
